﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;

using OpenCvSharp;

using Xc.DataAccess.CodeReader.Models.Filters;

using static System.Net.Mime.MediaTypeNames;

namespace Xc.Logics.CodeReaders.Models
{

    public class EditTemplateSelector : DataTemplateSelector
    {
        public Collection<EditTemplate> Templates { get; set; } = new Collection<EditTemplate>();
        /// <summary>
        /// 对象为枚举时调用该模板
        /// </summary>
        public DataTemplate EnumDataTemplate { get; set; } = new DataTemplate();
        /// <summary>
        /// 对象为彩色时调用该模板
        /// </summary>
        public DataTemplate ScalarDataTemplate { get; set; } = new DataTemplate();
        /// <summary>
        /// 默认模板
        /// </summary>
        public DataTemplate DefaultDataTemplate { get; set; }
        public override DataTemplate SelectTemplate(object item, DependencyObject container)
        {
            if (item == null)
            {
                return null;
            }
            if (item is InputParameter inputParameter)
            {
                var type = inputParameter.Type;

                return FinTemplate(type, EditDirection.Out) ?? DefaultDataTemplate;

            }
            else if (item is OutPutParameter outPutParameter)
            {
                var type = outPutParameter.Type;
                return FinTemplate(type, EditDirection.In) ?? DefaultDataTemplate;
            }
            return DefaultDataTemplate;
        }
        private EditTemplate FindTempalte(string type, EditDirection notIsDirection)
        {
            var d = Templates.Where(p => (p.Type.ToString() == type)  && p.Direction != notIsDirection).FirstOrDefault();
            return d;
        }
        private DataTemplate FinTemplate(Type type,EditDirection editDirection)
        {
            if (type == typeof(Scalar))
            {
                return ScalarDataTemplate;
            }


            if (type.IsGenericType||type.IsArray)
            {
                string source = "[]";
                string text = "";
                if (type.IsGenericType)
                {
                    text += source;
                }

            AGAIN:
                var c= type.GetGenericArguments().FirstOrDefault(); 
                if (c != null)
                {
                    type = c;
                }

                if (type.IsGenericType)
                {
                    text += source;
                    goto AGAIN;
                }
                else
                {
                    text = type.Name.Replace("`1", String.Empty) + text;
                    var t = FindTempalte(text, editDirection);
                    if (t != null)
                    {
                        return t.DataTemplate;
                    }
                }
            }
            else
            {
                if (type.IsEnum || type == typeof(bool))
                {
                    return EnumDataTemplate;
                }
                var d = FindTempalte(type.Name, editDirection);
                if (d != null)
                {
                    return d.DataTemplate;
                }
            }

            return null;
        }
    }
}
